<?php

namespace App\Console\Commands;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendBatchSmsRequest;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use App\Models\AuctionModel;
use App\Models\ShopUserModel;
use Exception;
use Illuminate\Console\Command;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;

class AuctionPushSmsMessageCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:auctionPushSmsMessage {content}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * $data[0] 拍卖商品id
     * $data[1] 发送范围1:全部 2:围观
     * $data[2] 模板类型:4:拍卖即将开始倒计时
     * $data[3] 消息类型:1:微信 2:短信 3:全部
     * $data[4] 时间值(单位：分钟)
     * Execute the console command.
     */
    public function handle()
    {
        if ($this->argument('content') === '') return;
        $data = explode('-', $this->argument('content'));

        $auction = AuctionModel::findByWhere(['id' => $data[0]]);
        if ((int)$data[1] === 1) {
            $user = ShopUserModel::query()->where(['user_status' => 1])
                ->get(['user_mobile', 'user_id'])->toArray();
        } else {
            $user = ShopUserModel::query()
                ->leftJoin('auction_follow', 'auction_follow.user_id', 'shop_user.user_id')
                ->where('auction_follow.auction_id', $data[0])
                ->where(['user_status' => 1, 'is_subscribe' => 1])->get(['user_mobile', 'user_id'])->toArray();
        }

        switch ($data[2]) {
            case 1:
                break;
            case 4:
//                self::timeCounting($user, $auction->title);
                self::main($user, $auction->title);
                break;
        }
    }

    /**
     *
     * @return void
     *
     */
    private function main(): void
    {
        $sms = config('sms');
        $config = new Config([
            "accessKeyId" => $sms['key'],
            "accessKeySecret" => $sms['secret']
        ]);
        $config->endpoint = "dysmsapi.aliyuncs.com";
        $client =new Dysmsapi($config);

        $phoneNumbers = [
            '17891626730',
//            '15267902931'
        ];

        $sendSmsRequest = new SendBatchSmsRequest([
            'phoneNumberJson' => json_encode($phoneNumbers),
            'TemplateCode' => $sms['auctionTimeCounting'],
            'SignNameJson' => json_encode(["异宠星球"], JSON_UNESCAPED_UNICODE),
            'TemplateParamJson' => json_encode([['name' => '爱你', 'num' => '一万年']], JSON_UNESCAPED_UNICODE)
        ]);
        try {
            $client->sendBatchSmsWithOptions($sendSmsRequest, new RuntimeOptions([]));
        }catch(Exception $exception) {
            if (!($exception instanceof TeaError)) {
                new TeaError([], $exception->getMessage(), $exception->getCode(), $exception);
            }
            var_dump($exception->message);
        }
    }
}
